package com.example.opengl.fbo

import android.view.Surface
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.baselibrary.base.BaseActivity
import com.android.baselibrary.video.VideoExt
import com.example.opengl.R
import com.example.opengl.databinding.ActivityFboRenderVideoBinding
import com.example.opengl.main.OpenGLPath
import com.example.opengl.main.OpenGlMainActivity
import com.example.opengl.player.base.BaseDecoder
import com.example.opengl.player.base.DefDecoderStateListener
import com.example.opengl.player.base.Frame
import com.example.opengl.player.decoder.AudioDecoder
import com.example.opengl.player.decoder.VideoDecoder
import com.example.opengl.render.video.VideoDrawer
import com.example.opengl.render.video.VideoRender
import java.util.concurrent.Executors

/**
 * FBO视频离屏渲染
 */
@Route(path = OpenGLPath.OPENGL_RENDER_FBO)
class FBORenderActivity:BaseActivity<ActivityFboRenderVideoBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_fbo_render_video
    private lateinit var drawer: FBOVideoDrawer
    private val path = OpenGlMainActivity.VIDEO_PATH

    override fun initView() {
      val videoInt =   VideoExt.getVideoInfo(path)
        drawer = FBOVideoDrawer()
        drawer.setVideoSize(videoInt.width,videoInt.height)
        drawer.getSurfaceTexture {
            initPlayer(Surface(it))
        }
        mBinding.surface.setEGLContextClientVersion(2)
        mBinding.surface.setRenderer(VideoRender(drawer))

    }

    private fun initPlayer(surface: Surface) {
        val threadPool = Executors.newFixedThreadPool(10)

        val videoDecoder = VideoDecoder(path,  surface)
        threadPool.execute(videoDecoder)

        val audioDecoder = AudioDecoder(path)
        threadPool.execute(audioDecoder)
        videoDecoder.goOn()
        audioDecoder.goOn()
    }

    override fun initListener() {

    }
}